﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Ultima.Xna.Input;
using System.Xml;
using Ultima.Xna.Data;

namespace Ultima.Xna.Graphics.UI
{
    public class GumpCheckbox : UINode, IGumpCheckbox
    {
        private int _uncheckedId;
        private int _checkedId;
        private int _switchId;

        private bool _checked;

        private Texture2D _uncheckedTexture;
        private Texture2D _checkedTexture;

        private Color[] _uncheckedTextureData;
        private Color[] _checkedTextureData;

        public Texture2D UncheckedTexture
        {
            get { return _uncheckedTexture; }
            set { _uncheckedTexture = value; }
        }

        public Texture2D CheckedTexture
        {
            get { return _checkedTexture; }
            set { _checkedTexture = value; }
        }

        public int UncheckedId
        {
            get { return _uncheckedId; }
            set { _uncheckedId = value; }
        }

        public int CheckedId
        {
            get { return _checkedId; }
            set { _checkedId = value; }
        }

        public int SwitchId
        {
            get { return _switchId; }
            set { _switchId = value; }
        }

        public virtual bool Checked
        {
            get { return _checked; }
            set { _checked = value; }
        }

        public override Vector2 Size
        {
            get
            {
                if (_checked)
                {
                    if (_checkedTexture != null)
                    {
                        return new Vector2(_checkedTexture.Width, _checkedTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
                else
                {
                    if (_uncheckedTexture != null)
                    {
                        return new Vector2(_uncheckedTexture.Width, _uncheckedTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
            }
            set { base.Size = value; }
        }

        public GumpCheckbox(Game game, IUIContainerNode parent)
            : base(game, parent)
        {

        }

        public override void Initialize(XmlElement element)
        {
            base.Initialize(element);

            _uncheckedTexture = Gumps.CreateTexture(_game.GraphicsDevice, _uncheckedId);
            _checkedTexture = Gumps.CreateTexture(_game.GraphicsDevice, _checkedId);

            _uncheckedTextureData = new Color[_uncheckedTexture.Width * _uncheckedTexture.Height];
            _checkedTextureData = new Color[_checkedTexture.Width * _checkedTexture.Height];

            _uncheckedTexture.GetData<Color>(_uncheckedTextureData);
            _checkedTexture.GetData<Color>(_checkedTextureData);
        }

        public override void HandleClick(MouseButtons button, bool down)
        {
            if (_defaultBehaviour)
            {
                if (!down)
                {
                    Checked = !_checked;
                }
            }
            else
            {
                base.HandleClick(button, down);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            RectangleF renderBounds;
            CalculateRenderBounds(out renderBounds);

            Texture2D texture = _uncheckedTexture;

            if (_checked)
                texture = _checkedTexture;

            _spriteBatch.Draw(texture, renderBounds, Color.White);

            base.Draw(gameTime);
        }

        public override void HitTest(ref float x, ref float y, out bool value)
        {
            base.HitTest(ref x, ref y, out value);

            if (value)
            {
                RectangleF renderBounds;
                CalculateRenderBounds(out renderBounds);

                float cx = x - renderBounds.X;
                float cy = y - renderBounds.Y;

                if (_checked)
                {
                    value = _checkedTextureData[(int)cx + ((int)cy * _checkedTexture.Width)].A > 0;
                }
                else
                {
                    value = _uncheckedTextureData[(int)cx + ((int)cy * _uncheckedTexture.Width)].A > 0;
                }
            }
        }
    }
}
